import time

from util.selenium.webdriver import ActionChains
from util.selenium.webdriver.common.by import By
from util.selenium.webdriver.support.select import Select
from util.selenium.webdriver.support.wait import WebDriverWait
from util.selenium.webdriver.support import expected_conditions as EC


def cut_over_frame(driver=None, selector_loc=''):
    if not driver:
        raise NameError('driver is not defind. custom--')
    if type(selector_loc) == 'str':
        driver.switch_to.frame(driver.find_element_by_css_selector(selector_loc))
    driver.switch_to.frame(driver.find_element(*selector_loc))


def select_value_choice(selector_loc, text_value, driver=None):
    if not driver:
        raise NameError('driver is not defind. custom--')
    text_value = str(text_value).replace(' ', '')
    if not text_value:
        return
    WebDriverWait(driver=driver, timeout=10, poll_frequency=0.5).until(EC.presence_of_all_elements_located(selector_loc))

    cer_body_select = driver.find_element(*selector_loc)
    option = driver.find_element(By.XPATH, "//*[text()='{}']".format(text_value))
    Select(cer_body_select).select_by_visible_text(text_value)
    # ActionChains(driver).move_to_element(cer_body_select).pause(0.2).click().move_to_element(option).pause(0.2).click().perform()
    time.sleep(0.2)

######### 保存按钮点击
def global_click_btn(btn_loc, driver=None, current_class=None):
    if not driver:
        raise NameError('driver is not defind. custom--')
    driver.find_element(*btn_loc).click()


# # # # 上传按钮点击
def click_upload_attach(btn_loc, driver=None, class_name=''):
    if not driver:
        raise NameError('driver is not defind. custom--')
    # te = driver.find_element(*btn_loc).text
    print(f'---上传--------'*10)

# # # # # # # # # #  返回按钮点击
# def click_back_btn(btn_loc, driver=None):
#     if not driver:
#         raise NameError('driver is not defind. custom--')
#     driver.find_element(*btn_loc).click()
#     pass

# def cut_tag_page_to_last_page(driver):
#     driver.switch_to.windows(driver.window_handles[-1])
